/*
 * Copyright (C) hushenglan
 */

#include "mynet.h"

namespace mynet
{

/*
 * 构造函数
 */
CEpollPoll::CEpollPoll(int maxOpenSockets, CServerSock *serverSock)
{
    _maxOpenSockets = maxOpenSockets;
    _events = (struct epoll_event*)malloc(sizeof(struct epoll_event) * _maxOpenSockets);
    _availEventNum = -1;
    _tmpEvent = new CEpollEvent();
    _serverSock = serverSock;
}

/*
 * 析构函数
 */
CEpollPoll::~CEpollPoll()
{
    free(_events);
    delete _tmpEvent;
}

/*
 * 创建事件模型
 */
bool CEpollPoll::create()
{
    _epollFd = epoll_create(_maxOpenSockets);
    if(_epollFd == -1)
    {
        /* error */
        return false;
    }

    return true;
}

/*
 * 等待事件
 */
int CEpollPoll::waitForEvents(int timeout)
{
    _availEventNum = epoll_wait(_epollFd, _events, _maxOpenSockets, timeout);

    return _availEventNum;
}

/*
 * 获取事件
 */
CEvent* CEpollPoll::getEvent(int index)
{
    if(index >= _availEventNum)
    {
        return NULL;
    }

    _tmpEvent.setEvent(&_events[index]);
}

}



